package ro.upt.dcda.pricezone.s1.requests;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import ro.upt.dcda.pricezone.common.ConnectionFactory;
import ro.upt.dcda.pricezone.s1.pojo.Product;

public class GetProductsRequest {

	private String name;
	private String description;

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@SuppressWarnings("unchecked")
	public List<Product> work() {
		Session session = ConnectionFactory.getFactory().openSession();
		try {
			Criteria crit = session.createCriteria(Product.class);
			crit.add(Restrictions.ilike("name", "%"
					+ (name == null ? "" : name) + "%"));
			crit.add(Restrictions.ilike("description", "%"
					+ (description == null ? "" : description) + "%"));
			System.out.println(name + " " + description);
			return (List<Product>) crit.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}
}
